<?php

namespace app\shop\model;

use app\BaseModel;
use app\channel\model\ChannelGoodsModel;
use app\channel\model\ChannelGoodsPricesModel;
use app\channel\model\ProxyLevelModel;
use app\channel\model\ProxyUserRegionModel;
use app\channel\model\ProxyUsersModel;
use app\channel\model\RewardModel;
use app\channel\model\StockDetailModel;
use app\channel\model\StockModel;
use app\channel\model\WalletModel;
use app\distribution\model\DividendModel;
use app\member\model\UsersModel;
use think\facade\Cache;
use think\Db;
use Wangdiantong\Wdtwms;

use app\member\model\AccountLogModel;
use app\member\model\AccountModel;

//*------------------------------------------------------ */
//-- 快递发货
/*------------------------------------------------------ */

class SendLogisticsModel extends BaseModel
{
    protected $table = 'shop_order_info';
    public $pk = 'order_id';
    protected $mkey = 'shop_order_mkey_';


    /**
     *发货接口
     */
    public function asynRunLogistics($param)
    {
        if ($param['order_id'] < 1) {
            return '缺少订单ID';
        }
        $param['order_id'] = $param['order_id'] * 1;//异步执行传入必须强制类型
        $orderInfo = $this->find($param['order_id'])->toArray();
        //获取仓库
        $warehouse = Db::name('shop_goods_warehouse')->where(array('id' => $orderInfo['warehouse_id']))->find();
        $shop_logistics_api = Db::name('shop_logistics_api')->where(array('id' => $warehouse['logistics_api']))->find();

        if ($shop_logistics_api) {
            $fun = str_replace('/', '\\', '/app/shop/controller/api/' . $shop_logistics_api['api_file']);
            $kd_api = new $fun();
            $result = $kd_api->sendCreateAdd($param['order_id']);

        }
        return true;
    }

    /**
     *发货接口
     */
    public function asynRunAgentLogistics($param)
    {

        if ($param['order_id'] < 1) {
            return '缺少订单ID';
        }
        $param['order_id'] = $param['order_id'] * 1;//异步执行传入必须强制类型
        $orderInfo = Db::name('channel_order_info')->where(array('order_id' => $param['order_id']))->find();

        if ($orderInfo) {
            //获取仓库
            $warehouse = Db::name('shop_goods_warehouse')->where(array('id' => $orderInfo['warehouse_id']))->find();
            $shop_logistics_api = Db::name('shop_logistics_api')->where(array('id' => $warehouse['logistics_api']))->find();

            if ($shop_logistics_api) {


                $fun = str_replace('/', '\\', '/app/shop/controller/api/' . $shop_logistics_api['api_file']);
                $kd_api = new $fun();
                $result = $kd_api->sendCreateAgentAdd($param['order_id']);
            }
        }
        return true;
    }


    /**
     * 商品入库
     */
    public function asynRunInGoods($param)
    {

        //获取仓库
        $warehouse = Db::name('shop_goods_warehouse')->where(array('id' => $param['warehouse_id']))->find();
        if ($warehouse) {
            $shop_logistics_api = Db::name('shop_logistics_api')->where(array('id' => $warehouse['logistics_api']))->find();

            $fun = str_replace('/', '\\', '/app/shop/controller/api/' . $shop_logistics_api['api_file']);
            $kd_api = new $fun();
            $result = $kd_api->inCrateAdd(array('goods_id' => $param['goods_id'], 'goods_number' => $param['goods_number']), 1);

        }

        return true;
    }

    /**
     * 查询商品;商品不存在则同步商品
     */
    public function asynRunSelectGoods($param)
    {

        //获取仓库
        $warehouse = Db::name('shop_goods_warehouse')->where(array('id' => $param['warehouse_id']))->find();

        if ($warehouse) {
            $shop_logistics_api = Db::name('shop_logistics_api')->where(array('id' => $warehouse['logistics_api']))->find();

            $fun = str_replace('/', '\\', '/app/shop/controller/api/' . $shop_logistics_api['api_file']);
            $kd_api = new $fun();
            $result = $kd_api->selectGoods($param['goods_id']);

        }
    }


}
